﻿namespace Centido.Core.Test
{
    using System;
    using System.Data.Common;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Byte array table column tests
    /// </summary>
    [TestClass]
    public class BinaryColumnTest : ColumnBaseTest<byte[]>
    {
        /// <summary>
        /// Binary table column
        /// </summary>
        private BinaryColumn Column
        {
            get { return GenericColumn as BinaryColumn; }
        }

        /// <summary>
        /// Get column type
        /// </summary>
        /// <returns></returns>
        protected override ColumnType GetColumnType()
        {
            return ColumnType.Binary;
        }

        /// <summary>
        /// Get large value
        /// </summary>
        /// <returns></returns>
        protected override byte[] GetLargeValue()
        {
            var random = new Random(Environment.TickCount);

            var value = new byte[50*1024];

            random.NextBytes(value);

            return value;
        }

        /// <summary>
        /// Get small data
        /// </summary>
        /// <returns></returns>
        protected override byte[] GetSmallValue()
        {
            return new byte[] {2, 234, 48};
        }

        /// <summary>
        /// Convert db value
        /// </summary>
        /// <param name="dbValue">Db value</param>
        /// <returns></returns>
        protected override byte[] ConvertDbValue(object dbValue)
        {
            return DbValue.GetBinary(dbValue);
        }

        /// <summary>
        /// Values are equal
        /// </summary>
        /// <param name="firstValue">First value</param>
        /// <param name="secondValue">Second value</param>
        /// <returns></returns>
        protected override bool ValuesAreEqual(object firstValue, object secondValue)
        {
            if (firstValue == null && secondValue == null)
                return true;
            if (firstValue == null)
                return false;
            if (!(firstValue is byte[]))
                return false;
            if (!(secondValue is byte[]))
                return false;
            
            return ((byte[])firstValue).SequenceEqual((byte[])secondValue);
        }

        /// <summary>
        /// Inserting large value
        /// </summary>
        [TestMethod]
        public void InsertingLargeValue()
        {
            var random = new Random(Environment.TickCount);

            var value = new byte[1024*1024];

            random.NextBytes(value);

            //Inserting the value
            var rowId = Table.InsertRow(GenericColumn.Value(value));

            Assert.AreEqual(1, Table.RowCount);

            //Reading inserted value
            var inserted = GenericColumn.EmptyValue();

            Table.ReadRow(rowId, inserted);

            Assert.IsNotNull(inserted.Value);
            Assert.AreEqual(inserted.GetBinary().Length, value.Length);
            Assert.IsTrue(inserted.GetBinary().SequenceEqual(value));
        }

        /// <summary>
        /// Inserting null value
        /// </summary>
        [TestMethod]
        public void InsertingNullValue()
        {
            //Inserting null value
            var rowId = Table.InsertRow(GenericColumn.Value(null));

            Assert.AreEqual(Table.RowCount, 1);

            Assert.IsNull(GenericColumn.Read(rowId).GetBinary());
        }


        /// <summary>
        /// Updating large value
        /// </summary>
        [TestMethod]
        public void UpdatingLargeValue()
        {
            var random = new Random(Environment.TickCount);

            var value = new byte[1024*1024];

            random.NextBytes(value);

            //Inserting null
            var rowId = Table.InsertRow(GenericColumn.Value(null));

            Assert.AreEqual(1, Table.RowCount);

            //Updating large value
            Table.UpdateRow(rowId, GenericColumn.Value(value));

            //Reading inserted value
            var updated = GenericColumn.EmptyValue();

            Table.ReadRow(rowId, updated);

            Assert.IsNotNull(updated.Value);
            Assert.AreEqual(updated.GetBinary().Length, value.Length);
            Assert.IsTrue(updated.GetBinary().SequenceEqual(value));
        }

        /// <summary>
        /// Updating column to null
        /// </summary>
        [TestMethod]
        public void UpdatingToNull()
        {
            //Inserting some value
            var rowId = Table.InsertRow(GenericColumn.Value(new byte[] {0, 1}));

            Assert.AreEqual(Table.RowCount, 1);

            Assert.IsNotNull(GenericColumn.Read(rowId).GetString());

            //Updating to null
            Table.UpdateRow(rowId, GenericColumn.Value(null));

            Assert.IsNull(GenericColumn.Read(rowId).GetString());
        }
    }
}